<template>
  <div v-show="$route.name == 'house-update'">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/house-list' }">仓库列表</el-breadcrumb-item>
      <el-breadcrumb-item>产品编辑</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-form :model="saveForm" :rules="saveFormRules" ref="saveForm" label-width="100px" class="demo-saveForm">
        <el-form-item label="类型" prop="product" style="width: 30%;">
          <el-input v-model="saveForm.product" readonly></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="productn" style="width: 30%;">
          <el-input v-model="saveForm.productn" readonly></el-input>
        </el-form-item>
        <el-form-item label="规格" prop="model" style="width: 30%;">
          <el-input v-model="saveForm.model" readonly></el-input>
        </el-form-item>
        <el-form-item label="单位" prop="unit" style="width: 30%;">
          <el-input v-model="saveForm.unit" readonly></el-input>
        </el-form-item>
        <el-form-item label="默认售价" prop="price"  style="width: 30%;">
          <el-input-number controls-position="right" :precision="2" :step="1" v-model="saveForm.price" placeholder="请输入价格" style="width:200px"></el-input-number>
        </el-form-item>
        <el-form-item>
          <el-button @click="back">返回</el-button>
          <el-button type="primary" @click="submitForm()">确定</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      id: '',
      saveForm: {
        product: '',
        productn: '',
        model: '',
        price: ''
      },
      houseData: {},
      saveFormRules: {
        price: [{ required: true, message: '请输入默认售价', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.getHouseData()
  },
  methods: {
    async submitForm() {
      this.$refs.saveForm.validate(async (valid) => {
        if (!valid) return
        // 可以发起新增入库单的网络请求
        const { data: result } = await this.$http.put('/house/' + this.id, {
          data: this.saveForm
        })
        if (result.status !== 200) {
          return this.$message.error(result.msg)
        }
        this.$message.success('修改成功！')
        this.$router.push({ path: '/house-list' })
      })
    },
    getHouseData() {
      if (this.$route.name === 'house-update') {
        if (this.$route.params.id) {
          this.id = this.$route.params.id
          localStorage.setItem('HouseId', this.$route.params.id)
        } else {
          this.id = localStorage.getItem('HouseId')
        }
        this.getHouse()
      }
    },
    async getHouse() {
      // 发起编辑的网络请求
      const { data: result } = await this.$http.get('/house/' + this.id)
      if (result.status !== 200) {
        return this.$message.error(result.msg)
      }
      this.saveForm = result.data
    },
    // 返回
    back() {
      localStorage.removeItem('HouseId')
      this.$router.push({ 'name': 'house-list' })
    }
  }
}
</script>
